@echo off
setlocal

cd /d %~dp0

if "%1" == "" goto :usage

set distro=%1

if "%distro:~0,1%" == "-" goto :usage
if "%distro:~0,1%" == "/" goto :usage

if "%2" == "" (
	set seed=alpine
) else (
	set seed=%2
)

if exist %distro% (
	echo alrealdy exist: %distro%
	goto :eof
)

if NOT exist .seed\%seed%.rootfs.tar.gz (
	echo seed OS not exist: %seed%
	echo valid seeds: ^(default alpine^)
	for /f "tokens=1 delims=." %%i in ('dir /b .seed\*.rootfs.tar.gz') do @echo     %%i
	goto :eof
)

mkdir %distro% && ^
mklink /H %distro%\%distro%.exe .seed\distro.exe && ^
mklink /H %distro%\rootfs.tar.gz .seed\%seed%.rootfs.tar.gz && ^
%distro%\%distro%.exe && ^
if exist .seed\%seed%.init.sh wsl -d %distro% -- sh .seed/%seed%.init.sh && ^
mklink /H %distro%.exe .seed\distro.exe && ^
%distro%.exe
goto :eof

:usage
echo USAGE:
echo   %0 add DISTRO [OS]  create the distro
echo default OS is alpine if omitted.
